﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLToolkit.Data;
using BLToolkit.Data.DataProvider;
using BLToolkit.DataAccess;
using BLToolkit.Mapping;
using BLToolkit.Reflection.Extension;

namespace Devsoft.DB.Models.BltookitEx
{
    class SqlQueryEx<T> : SqlQuery<T>
    {
        public SqlQueryEx(DbManager db)
            : base(db)
        {

        }

        public virtual object InsertWithIdentity(T obj)
        {
            DbManager db = GetDbManager();

            try
            {
                SqlQueryInfo query = GetSqlQueryInfo(db, obj.GetType(), "Insert");
                string sql = string.Format("{0}; {1};", query.QueryText, "SELECT SCOPE_IDENTITY()");
                return db
                    .SetCommand(sql, query.GetParameters(db, obj))
                    .ExecuteScalar<long>();
            }
            finally
            {
                if (DisposeDbManager)
                    db.Dispose();
            }
        }
    }
}
